<?php
namespace Modules\ShopApi\Models;
use Tymon\JWTAuth\Contracts\JWTSubject;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Modules\ShopApi\Models\BaseApiModel;
use Modules\ShopApi\Services\ShopUserService;
use Modules\ShopApi\Models\ShopProductCollection as ShopProductCollectionModel;
use Modules\ShopApi\Models\ShopProductFootprint as ShopProductFootprintModel;
use Modules\ShopApi\Models\ShopOrder as ShopOrderModel;
use Modules\ShopApi\Models\ShopPictureCollection as ShopPictureCollectionModel;
class ShopUser extends Authenticatable implements JWTSubject 
{ 
	use Notifiable;
	protected $guard = 'shop_user';
	protected $hidden = [
		'password'
	];
	/**
     * @name  jwt标识 
     */
	public function getJWTIdentifier()
    {
        return $this->getKey();
    }
	/**
     * @name  jwt自定义声明 
     */
    public function getJWTCustomClaims()
    {
        return [];
    }
	/**
     * @name  用户登录 
     */
	public function login($data){
		if(isset($data['type']) && $data['type'] == 2){
			return $this->tt_login($data);
		}else{
			return $this->wx_login($data);
		} 
	}
	/**
     * @name  字节小程序登录 
     */
	private function tt_login($data){
		$ShopUserService = new ShopUserService(); 
		$user_info = $ShopUserService->get_user_info($data);
		$BaseApiModel = new BaseApiModel();  
		$id = $this->where(['xcx_open_id'=>$user_info['xcx_open_id']])->value('id');
		if(!$id){
			$user_info['password'] =  bcrypt(config('shopapi.user_password'));
			$id = $this->insertGetId($user_info);
		} 
		if($id){
			$token = $ShopUserService->set_token(['id'=>$id,'password'=>config('shopapi.user_password')]);
			if(count($token)){
				return $BaseApiModel->api_success([
					'msg'=>'登录成功！',
					'data'=>$token
				]);
			}
		}
		return $BaseApiModel->api_error([
			'msg'=>'登录失败！'
		]);
	}
	/**
     * @name  微信小程序登录 
     */
	private function wx_login($data){
		$ShopUserService = new ShopUserService();
		$user_info = $ShopUserService->get_user_info($data);
		$BaseApiModel = new BaseApiModel();
		$user_arr = [];
		//是否登录失败
		if(isset($user_info['code'])){
			return $BaseApiModel->api_error($user_info);
		}
		//判断是否存在union_id
		if(isset($user_info['unionId'])){
			$id = $this->where(['union_id'=>$user_info['unionId']])->value('id');
		}else{
			$id = $this->where(['xcx_open_id'=>$user_info['openId']])->value('id');
		}
		$user_arr = [
			'xcx_open_id'=>$user_info['openId'],
			'nick_name'=>$user_info['nickName'],
			'avatar_url'=>$user_info['avatarUrl'],
			'gender'=>$user_info['gender']
		];
		 
		if(isset($user_info['unionId'])){
			$user_arr['union_id'] = $user_info['unionId'];
		}
		if(!$id){
			$user_arr['password'] =  bcrypt(config('shopapi.user_password'));
			$user_arr['created_at'] =  date('Y-m-d H:i:s');
			$id = $this->insertGetId($user_arr);
		}else{
			$this->where(['id'=>$id])->update($user_arr);	 
		}
		if($id){
			$token = $ShopUserService->set_token(['id'=>$id,'password'=>config('shopapi.user_password')]);
			if(count($token)){
				return $BaseApiModel->api_success([
					'msg'=>'登录成功！',
					'data'=>$token
				]);
			}
		}
		return $BaseApiModel->api_error([
			'msg'=>'登录失败！'
		]);
	}
	/**
     * @name  刷新token 
     */
	public function refresh(){
		$token = (new ShopUserService())->refresh();
        return (new BaseApiModel())->api_success(['data'=>$token]);
	}
	/**	 
	 * @name  获取用户信息	 
	 */
	public function my(){
		$user_info = (new ShopUserService())->my();
		if($user_info==false){
			$data = [
				'user_status'=>0,
				'nick_name'=>'',
				'phone'=>'',
				'avatar_url'=>'',
				'footprint_count'=>0,
				'my_live_count'=>0,
				'collection_count'=>0,
				'order_count1'=>0,
				'order_count2'=>0,
				'order_count3'=>0,
				'order_count4'=>0,
				'order_count5'=>0,
			];
		}else{
			$order_count1 = 0;
			$order_count2 = 0;
			$order_count3 = 0;
			$order_count4 = 0;
			$order_count5 = 0;
			$order_list = ShopOrderModel::select('id','status')->where(['user_id'=>$user_info['id']])->whereIn('status',[1,2,3,4,6,7,8])->get()->toArray();
			foreach($order_list as $k=>$v){
				if($v['status'] == 1){
					$order_count1 = $order_count1 + 1;
				}else if($v['status'] == 2){
					$order_count2 = $order_count2 + 1;
				}else if($v['status'] == 3){
					$order_count3 = $order_count3 + 1;
				}else if($v['status'] == 4){
					$order_count4 = $order_count4 + 1;
				}else if(in_array($v['status'],[6,7,8])){
					$order_count5 = $order_count5 + 1;
				}
			}
			$data = [
				'user_status'=>1,
				'nick_name'=>$user_info['nick_name'],
				'phone'=>$user_info['phone'],
				'avatar_url'=>$user_info['avatar_url'],
				'footprint_count'=>ShopProductFootprintModel::where(['user_id'=>$user_info['id']])->whereHas('product_one',function($query){
					$query->where('status',1);
				})->count(),
				'collection_count'=>ShopProductCollectionModel::where(['user_id'=>$user_info['id'],'status'=>1])->whereHas('product_one',function($query){
					$query->where('status',1);
				})->count(),
				'my_live_count'=>ShopPictureCollectionModel::where(['user_id'=>$user_info['id'],'status'=>1])->whereHas('picture_one',function($query){
					$query->where(['type'=>3,'status'=>1]); 
				})->count(),
				'order_count1'=>$order_count1,
				'order_count2'=>$order_count2,
				'order_count3'=>$order_count3,
				'order_count4'=>$order_count4,
				'order_count5'=>$order_count5,
			];
		};
		return (new BaseApiModel())->api_success(['data'=>$data]);
	}	
	/**	 
	 * @name  获取设置信息	 
	 */
	public function set(){
		$user_info = (new ShopUserService())->my();
		$data = [
			'nick_name'=>$user_info['nick_name'],
			'phone'=>$user_info['phone'],
			'avatar_url'=>$user_info['avatar_url']
		];
		return (new BaseApiModel())->api_success(['data'=>$data]);
	}
	/**	 
	 * @name  绑定手机号	 
	 */
	public function send_phone($data){
		$ShopUserService = new ShopUserService(); 
		$phone_data = $ShopUserService->send_phone($data); 
		$user_info = $ShopUserService->my();
		$phone_data = json_decode($phone_data,true);
		if($this->where(['id'=>$user_info['id']])->update(['phone'=>$phone_data['purePhoneNumber']])!==false){
			return (new BaseApiModel())->api_success();
		};
		return (new BaseApiModel())->api_error();
	}
	/**	 
	 * @name  退出登录
	 */
    public function logout()
    {
		(new ShopUserService())->logout();
		return (new BaseApiModel())->api_success(['msg'=>'退出成功！']);
    }	
}
